/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shift.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.shift.entity.Shift;
import com.thinkgem.jeesite.modules.shift.service.ShiftService;

/**
 * 班次Controller
 * 
 * @author hui.li
 * @version 2017-07-22
 */
@Controller
@RequestMapping(value = "${adminPath}/shift/shift")
public class ShiftController extends BaseController {

	@Autowired
	private ShiftService shiftService;

	@ModelAttribute
	public Shift get(@RequestParam(required = false) String id) {
		Shift entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = shiftService.get(id);
		}
		if (entity == null) {
			entity = new Shift();
		}
		return entity;
	}

	@RequiresPermissions("shift:shift:view")
	@RequestMapping(value = { "list", "" })
	public String list(Shift shift, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<Shift> page = shiftService.findPage(new Page<Shift>(request,
				response), shift);
		model.addAttribute("page", page);
		return "modules/shift/shiftList";
	}

	@RequiresPermissions("shift:shift:view")
	@RequestMapping(value = "form")
	public String form(Shift shift, Model model) {

	    /*Map<String, String> roleMap = new HashMap<String, String>();
	    roleMap.put("3", "星期三");  */

	    String weekStr = shift.getWeek();

	    List<String> weekList = new ArrayList<String>();

	    if(StringUtils.isNotBlank(weekStr)){
	       String [] weekArray = weekStr.split(",");
	       Collections.addAll(weekList, weekArray);
	       shift.setWeekList(weekList);
        }
		model.addAttribute("shift", shift);
	 /*  model.addAttribute("weekList",weekList);*/
		return "modules/shift/shiftForm";
	}

	@RequiresPermissions("shift:shift:edit")
	@RequestMapping(value = "save")
	public String save(Shift shift, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, shift)) {
			return form(shift, model);
		}
		shiftService.save(shift);
		addMessage(redirectAttributes, "保存班次成功");
		return "redirect:" + Global.getAdminPath() + "/shift/shift/?repage";
	}

	@RequiresPermissions("shift:shift:edit")
	@RequestMapping(value = "delete")
	public String delete(Shift shift, RedirectAttributes redirectAttributes) {
		shiftService.delete(shift);
		addMessage(redirectAttributes, "删除班次成功");
		return "redirect:" + Global.getAdminPath() + "/shift/shift/?repage";
	}

	/**
	 * 异步调用，查询班次段列表
	 * 
	 * @param Schclass
	 * @param response
	 * @return
	 */
	@RequiresPermissions("shift:shift:view")
	@ResponseBody
	@RequestMapping(value = { "getShiftList" })
	public Map<String, Object> getSchclassList(Shift shift,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		response.setContentType("application/json; charset=UTF-8");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Page<Shift> page = shiftService.findPage(new Page<Shift>(request,
					response), shift);
			if (null == page) {
				resultMap.put("success", false);
			} else {
				resultMap.put("success", true);
				if (null != page.getList() && page.getList().size() > 0) {
					resultMap.put("data", page);
					model.addAttribute("page", page);
				}
			}
		} catch (Exception e) {
			resultMap.put("success", false);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 获取班次JSON数据。
	 * 
	 * @param
	 * @param response
	 * @return
	 */
	@RequiresPermissions("shift:shift:view")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(String shiftId) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Shift> list = shiftService.findList(new Shift());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < list.size(); i++) {
			Shift e = list.get(i);
			if(!e.getId().equals(shiftId))
			{
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				String name = e.getName() + "|" + sdf.format(e.getStartdate())
						+ "|" + sdf.format(e.getEnddate());
				map.put("name", name);
				mapList.add(map);
			}	
		}
		return mapList;
	}

}